<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      :inline="true"
      v-show="showSearch"
      label-width="88px"
      class="formList"
    >
      <el-row>
        <!-- <el-col :span="5"> -->
          <el-form-item label="供应商" prop="supplierUid">
            <el-select
              v-model="queryParams.supplierUid"
              placeholder="请选择供应商"
              clearable
            >
              <el-option
                v-for="dict in supplierUidOptions"
                :key="dict.supplierUid"
                :label="dict.supplierName"
                :value="dict.supplierUid"
              />
            </el-select>
          </el-form-item>
        <!-- </el-col>
        <el-col :span="5"> -->
          <el-form-item label="负责人姓名" prop="name">
            <el-input
              v-model="queryParams.name"
              placeholder="请输入负责人姓名"
              clearable
              size="small"
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
        <!-- </el-col>
        <el-col :span="5"> -->
          <el-form-item label="负责人电话" prop="mobile">
            <el-input
              v-model="queryParams.mobile"
              placeholder="请输入负责人电话"
              clearable
              size="small"
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
        <!-- </el-col> -->

      </el-row>
      <el-row>
        <!-- <el-col :span="5"> -->
          <el-form-item label="是否默认" prop="isDefault">
            <el-select
              v-model="queryParams.isDefault"
              placeholder="请选择是否默认"
              clearable
              size="small"
            >
              <el-option
                v-for="dict in isDefaultOptions"
                :key="dict.dictValue"
                :label="dict.dictLabel"
                :value="dict.dictValue"
              />
            </el-select>
          </el-form-item>
        <!-- </el-col>

        <el-col :span="5"> -->
          <el-form-item label="数据状态" prop="dataStatus">
            <el-select
              v-model="queryParams.dataStatus"
              placeholder="请选择数据状态"
              clearable
              size="small"
            >
              <el-option
                v-for="dict in dataStatusOptions"
                :key="dict.dictValue"
                :label="dict.dictLabel"
                :value="dict.dictValue"
              />
            </el-select>
          </el-form-item>
        <!-- </el-col>
        <el-col :span="5"> -->
          <el-form-item label="地址类型" prop="dataType">
            <el-select
              v-model="queryParams.dataType"
              placeholder="请选择地址类型"
              clearable
              size="small"
            >
              <el-option
                v-for="dict in dataTypeOptions"
                :key="dict.dictValue"
                :label="dict.dictLabel"
                :value="dict.dictValue"
              />
            </el-select>
          </el-form-item>
        <!-- </el-col> -->
      </el-row>

      <el-row>
        <el-form-item style="margin-left: 20px">
            <el-button
              type="primary"
              icon="el-icon-search"
              size="mini"
              @click="handleQuery"
            >搜索
            </el-button
            >
            <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
            >重置
            </el-button
            >
          </el-form-item>
      </el-row>
    </el-form>
    <el-row class="row-mbinfo" style="margin-left: 0px; margin-right: 0px;">
      <el-row>
        <el-col :span="1.5">
          <el-button
            type="primary"
            plain
            icon="el-icon-plus"
            size="mini"
            @click="handleAdd"
            v-hasPermi="['supplier:address:add']"
          >新增
          </el-button>
        </el-col>
        <el-col :span="1.5">
          <!--          <el-button-->
          <!--            type="success"-->
          <!--            plain-->
          <!--            icon="el-icon-edit"-->
          <!--            size="mini"-->
          <!--            :disabled="single"-->
          <!--            @click="handleUpdate"-->
          <!--            v-hasPermi="['supplier:address:edit']"-->
          <!--          >修改-->
          <!--          </el-button>-->
        </el-col>
        <el-col :span="1.5" style="margin-left: 10px">
          <el-button
            type="danger"
            plain
            icon="el-icon-delete"
            size="mini"
            :disabled="multiple"
            @click="handleDelete"
            v-hasPermi="['supplier:address:remove']"
          >删除
          </el-button>
        </el-col>
        <el-col :span="1.5">
          <!--          <el-button-->
          <!--            type="warning"-->
          <!--            plain-->
          <!--            icon="el-icon-download"-->
          <!--            size="mini"-->
          <!--            @click="handleExport"-->
          <!--            v-hasPermi="['supplier:address:export']"-->
          <!--          >导出-->
          <!--          </el-button>-->
        </el-col>
        <right-toolbar
          :showSearch.sync="showSearch"
          @queryTable="getList"
        ></right-toolbar>
      </el-row>
    </el-row>

    <div class="divTable">
      <el-table
        border
        v-loading="loading"
        :data="addressList"
        @selection-change="handleSelectionChange"
      >
        <el-table-column type="selection" width="55" align="center"/>
        <el-table-column
          label="供应商名称"
          align="center"
          prop="supplierName"
          min-width="120"
          :show-overflow-tooltip="true"
        />
        <el-table-column
          label="负责人姓名"
          align="center"
          prop="name"
          min-width="100"
          :show-overflow-tooltip="true"
        />
        <el-table-column
          label="负责人电话"
          align="center"
          prop="mobile"
          min-width="100"
          :show-overflow-tooltip="true"
        />
        <el-table-column
          label="是否默认"
          align="center"
          prop="isDefault"
          :formatter="isDefaultFormat"
        />
        <el-table-column label="状态" align="center" prop="dataStatus">
          <template slot-scope="scope">
            <el-switch
              v-model="scope.row.dataStatus"
              active-value="1"
              inactive-value="0"
              @change="handleStatusChange(scope.row)"
            ></el-switch>
          </template>
        </el-table-column>
        <el-table-column
          label="地址类型"
          align="center"
          prop="dataType"
          :formatter="dataTypeFormat"
        />
        <el-table-column
          label="操作"
          align="center"
          class-name="small-padding fixed-width"
          min-width="120"
          fixed="right"
        >
          <template slot-scope="scope">
            <el-button
              size="mini"
              type="text"
              icon="el-icon-zoom-in"
              @click="handleShowDetail(scope.row)"
              v-hasPermi="['supplier:address:query']"
            >查看
            </el-button>
            <el-button
              size="mini"
              type="text"
              icon="el-icon-edit"
              @click="handleUpdate(scope.row)"
              v-hasPermi="['supplier:address:edit']"
            >修改
            </el-button>
            <el-button
              size="mini"
              type="text"
              icon="el-icon-delete"
              @click="handleDelete(scope.row)"
              v-if="scope.row.dataStatus == '0'"
              v-hasPermi="['supplier:address:remove']"
            >删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>

      <pagination
        v-show="total > 0"
        :total="total"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="getList"
      />
    </div>

    <!-- 添加或修改供应商地址对话框 -->
    <el-dialog
      :title="title"
      :visible.sync="open"
      width="500px"
      append-to-body
      @keyup.enter.native="submitForm"
    >
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="供应商" prop="supplierUid">
          <el-select style="width: 100%" v-model="form.supplierUid" placeholder="请选择供应商">
            <el-option
              v-for="dict in supplierUidOptions"
              :key="dict.supplierUid"
              :label="dict.supplierName"
              :value="dict.supplierUid"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="负责人姓名" prop="name">
          <el-input v-model="form.name" placeholder="请输入负责人姓名"/>
        </el-form-item>
        <el-form-item label="负责人电话" prop="mobile">
          <el-input v-model="form.mobile" placeholder="请输入负责人电话"/>
        </el-form-item>
        <el-form-item label="地区" prop="region">
          <el-cascader style="width: 100%" clearable v-model="form.region" :options="options" ref="cascaderAddr"></el-cascader>
        </el-form-item>
        <el-form-item label="详细地址" prop="address">
          <el-input v-model="form.address" placeholder="请输入详细地址"/>
        </el-form-item>
        <el-form-item label="邮政编号" prop="postCode">
          <el-input v-model="form.postCode" placeholder="请输入邮政编号"/>
        </el-form-item>
        <el-form-item label="是否默认">
          <el-radio-group v-model="form.isDefault">
            <el-radio
              v-for="dict in isDefaultOptions"
              :key="dict.dictValue"
              :label="dict.dictValue"
            >{{ dict.dictLabel }}
            </el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="地址类型" prop="dataType">
          <el-select style="width: 100%" v-model="form.dataType" placeholder="请选择地址类型">
            <el-option
              v-for="dict in dataTypeOptions"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="dict.dictValue"
            ></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 查看供应商地址对话框 -->
    <el-dialog
      :title="viewTitle"
      :visible.sync="viewOpen"
      width="700px"
      append-to-body
    >
      <el-form ref="detailForm">
        <div>
          <!-- <i class="MmsUiBlockTitle___line___27lhp1-44-1"></i> -->
          <div class="big_title_style" style="font-size: 18px">
            <span>地址信息</span>
          </div>
        </div>
        <el-divider></el-divider>
        <div style="margin-left: 50px">
          <el-row>
            <el-col :span="12">
              <el-form-item>
                <el-form-item label="地址类型">
                  <span>{{ showDataType }}</span>
                </el-form-item>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item>
                <el-form-item label="是否默认">
                  <span>{{ showDataIsDefault }}</span>
                </el-form-item>
              </el-form-item>
            </el-col>

          </el-row>
          <el-row>
            <el-col :span="12">
              <el-form-item>
                <el-form-item label="负责人姓名">
                  <span>{{ showData.name }}</span>
                </el-form-item>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item>
                <el-form-item label="负责人电话">
                  <span>{{ showData.mobile }}</span>
                </el-form-item>
              </el-form-item>
            </el-col>

          </el-row>
          <el-row>
            <el-col :span="12">
              <el-form-item>
                <el-form-item label="邮政编号">
                  <span>{{ showData.postCode }}</span>
                </el-form-item>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item>
                <el-form-item label="地区">
                  <span>{{ showData.provinceName }} {{ showData.cityName }} {{ showData.countyName }} {{ showData.address  }}</span>
                </el-form-item>
              </el-form-item>
            </el-col>
          </el-row>
        </div>

        <div>
          <!-- <i class="MmsUiBlockTitle___line___27lhp1-44-1"></i> -->
          <div class="big_title_style" style="font-size: 18px">
            <span>其他信息</span>
          </div>
        </div>
        <el-divider></el-divider>
        <div style="margin-left: 50px">
          <el-row>
            <el-col :span="12">
              <el-form-item label="更新时间">
                <span>{{ showData.updateTime }}</span>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="更新人账号">
                <span>{{ showData.updateName }}</span>
              </el-form-item>
            </el-col>
          </el-row>

          <el-row>
            <el-col :span="12">
              <el-form-item label="创建时间">
                <span>{{ showData.createTime }}</span>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="创建人账号">
                <span>{{ showData.createName }}</span>
              </el-form-item>
            </el-col>
          </el-row>

        </div>


      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button type="primary" size="mini" @click="handCloseDetail"
        >确 定</el-button
        >
      </span>
    </el-dialog>
  </div>
</template>

<script>
  import {
    listAddress,
    getAddress,
    delAddress,
    addAddress,
    updateAddress,
    updateDataStatus
  } from '@/api/supplier/address'

  import {getRegion} from '@/api/common/regin'
  import {getSupplierList} from '@/api/supplier/info'
  //验证手机号
  let checkPhone = (rule, value, callback) => {
    if (value == null || value == ''){
      callback()
    }
    let reg =
      /^((\d{11})|^((\d{7,8})|(\d{4}|\d{3})-(\d{7,8})|(\d{4}|\d{3})-(\d{7,8})-(\d{4}|\d{3}|\d{2}|\d{1})|(\d{7,8})-(\d{4}|\d{3}|\d{2}|\d{1}))$)$/
    if (!reg.test(value)) {
      callback(new Error('手机号或电话输入错误'))
    } else {
      callback()
    }
  }
  export default {
    name: 'Address',
    components: {},
    data() {
      return {
        supplierUidOptions:'',
        //收货类型翻译
        showDataType: '',
        //是否默认翻译
        showDataIsDefault: '',
        // 地区信息
        options: [],
        // 遮罩层
        loading: true,
        // 选中数组
        ids: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 显示搜索条件
        showSearch: true,
        // 总条数
        total: 0,
        // 供应商地址表格数据
        addressList: [],
        // 供应商列表
        supplierNameOptions: [],
        // 弹出层标题
        title: '',
        //查看详细标题
        viewTitle: '',
        //选中数组的名字
        names: [],
        // 是否显示弹出层
        open: false,
        //是否显示查看详细弹出层
        viewOpen: false,
        //查看详细数据
        showData: [],
        // 是否默认字典
        isDefaultOptions: [],
        // 数据状态字典
        dataStatusOptions: [],
        // 地址类型字典
        dataTypeOptions: [],
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          name: null,
          mobile: null,
          isDefault: null,
          dataStatus: null,
          dataType: null
        },
        columns: [
          {
            label: '地址类型',
            dataIndex: 'supplierAddressUid'
          },
          {
            label: '平台ID',
            dataIndex: 'platformUid'
          },
          {
            label: '供应商ID',
            dataIndex: 'supplierUid'
          },
          {
            label: '负责人姓名',
            dataIndex: 'name'
          },
          {
            label: '负责人电话',
            dataIndex: 'mobile'
          },
          {
            label: '省份代码',
            dataIndex: 'provinceCode'
          },
          {
            label: '省份名称',
            dataIndex: 'provinceName'
          },
          {
            label: '城市代码',
            dataIndex: 'cityCode'
          },
          {
            label: '城市名称',
            dataIndex: 'cityName'
          },
          {
            label: '区县代码',
            dataIndex: 'countyCode'
          },
          {
            label: '区县名称',
            dataIndex: 'countyName'
          },
          {
            label: '详细地址',
            dataIndex: 'address'
          },
          {
            label: '邮政编号',
            dataIndex: 'postCode'
          },
          {
            label: '是否默认',
            dataIndex: 'isDefault'
          },
          {
            label: '是否默认',
            dataIndex: 'createTime'
          },
          {
            label: '是否默认',
            dataIndex: 'updateTime'
          },
          {
            label: '是否默认',
            dataIndex: 'updateUid'
          },
          {
            label: '数据状态',
            dataIndex: 'dataStatus'
          },
          {
            label: '地址类型',
            dataIndex: 'dataType'
          },
          {
            label: '地址',
            dataIndex: 'region'
          }
        ],
        // 表单参数
        form: {},
        // 表单校验
        rules: {
          supplierUid:[
            {required: true, message: "请选择供应商", trigger: "change"},
          ],
          name: [
            {required: true, message: '负责人姓名不能为空', trigger: 'blur'},
            { min: 1, max: 15, message: '负责人姓名 1 到 15个字符之间', trigger: 'blur' }

          ],
          mobile: [
            {required: true, message: '负责人电话不能为空', trigger: 'blur'},
            {
              type: 'number',
              validator: checkPhone,
              message: '输入有效手机号或电话',
              trigger: ['blur', 'change']
            },
          ],
          isDefault: [
            {required: true, message: '是否默认不能为空', trigger: 'blur'},
          ],
          dataStatus: [
            {required: true, message: '数据状态不能为空', trigger: 'blur'}
          ],
          dataType: [
            {required: true, message: '地址类型不能为空', trigger: 'change'}
          ],
          address: [
            {required: true, message: '详细地址不能为空', trigger: 'blur'},
            { min: 1, max: 30, message: '详细地址 1 到 30个字符之间', trigger: 'blur' }
          ],
          region: [{required: true, message: '请选择地区', trigger: 'blur'}],
          postCode:[
            { min: 6, max: 6, message: '请输入正确的邮政编码', trigger: 'blur' }
          ]
        },
      }
    },
    created() {
      this.getList()


      this.getDicts('supplier_default_address').then((response) => {
        this.isDefaultOptions = response.data
      })
      this.getDicts('sys_data_status').then((response) => {
        this.dataStatusOptions = response.data
      })
      this.getDicts('supplier_address_type').then((response) => {
        this.dataTypeOptions = response.data
      })
    },
    //生命周期 - 挂载完成（可以访问DOM元素）
    mounted() {
      getSupplierList(1).then((response) => {
        this.supplierUidOptions = response.data
      })
    },
    methods: {
      /** 查询供应商地址列表 */
      getList() {
        this.loading = true
        listAddress(this.queryParams).then((response) => {
          this.addressList = response.rows
          this.total = response.total
          this.loading = false
        })
      },
      // 是否默认字典翻译
      isDefaultFormat(row, column) {
        return this.selectDictLabel(this.isDefaultOptions, row.isDefault)
      },
      // 数据状态字典翻译
      dataStatusFormat(row, column) {
        return this.selectDictLabel(this.dataStatusOptions, row.dataStatus)
      },
      // 地址类型字典翻译
      dataTypeFormat(row, column) {
        return this.selectDictLabel(this.dataTypeOptions, row.dataType)
      },
      // 取消按钮
      cancel() {
        this.open = false
        this.reset()
      },
      // 表单重置
      reset() {
        this.form = {
          supplierAddressUid: null,
          platformUid: null,
          supplierUid: null,
          name: null,
          mobile: null,
          provinceCode: null,
          provinceName: null,
          cityCode: null,
          cityName: null,
          countyCode: null,
          countyName: null,
          address: null,
          postCode: null,
          isDefault: '0',
          createTime: null,
          updateTime: null,
          updateUid: null,
          dataStatus: '0',
          dataType: null,
          region: null
        }
        this.resetForm('form')
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1
        this.getList()
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.resetForm('queryForm')
        this.handleQuery()
      },
      // 多选框选中数据
      handleSelectionChange(selection) {
        this.ids = selection.map((item) => item.supplierAddressUid)
        this.names = selection.map((item) => item.name)
        this.single = selection.length !== 1
        this.multiple = !selection.length
        //用于禁用删除
        if (this.multiple == false) {
          for (var i = 0; i < selection.length; i++) {
            var element = selection[i]
            if ('1' === element.dataStatus) {
              this.multiple = true
              break
            }
          }
        }
      },
      /** 新增按钮操作 */
      handleAdd() {
        //获取地区信息
        getRegion().then((response) => {
          this.options = response.data
        })
        this.reset()
        this.open = true
        this.title = '添加供应商地址'
      },
      /** 查看详情按钮操作 **/
      handleShowDetail(row) {
        this.viewOpen = true
        this.viewTitle = '查看供应商地址'
        this.showData = row

        let this_ = this;
        this.dataTypeOptions.forEach(function (value, key) {
          if (value.dictValue == row.dataType) {
            this_.showDataType = value.dictLabel
            return false
          }
        })

        this.isDefaultOptions.forEach(function (value, key) {
          if (value.dictValue == row.isDefault) {
            this_.showDataIsDefault = value.dictLabel
            return false
          }
        })

      },
      handCloseDetail() {
        this.viewOpen = false
        this.showData = []
      },
      /** 修改按钮操作 */
      handleUpdate(row) {
        this.reset()
        //获取地区信息
        getRegion().then((response) => {
          this.options = response.data
        })
        const supplierAddressUid = row.supplierAddressUid || this.ids
        getAddress(supplierAddressUid).then((response) => {
          this.form = response.data
          this.open = true
          this.title = '修改供应商地址'
        })
      },
      /** 提交按钮 */
      submitForm() {
        this.$refs['form'].validate((valid) => {
          let areaCode = this.$refs['cascaderAddr'].getCheckedNodes()[0].path
          this.form.provinceCode = areaCode[0]
          this.form.cityCode = areaCode[1]
          this.form.countyCode = areaCode[2]
          let areaName = this.$refs['cascaderAddr'].getCheckedNodes()[0].pathLabels
          this.form.provinceName = areaName[0]
          this.form.cityName = areaName[1]
          this.form.countyName = areaName[2]
          if (valid) {
            if (this.form.supplierAddressUid != null) {
              updateAddress(this.form).then((response) => {
                this.msgSuccess('修改成功')
                this.open = false
                this.getList()
              })
            } else {
              addAddress(this.form).then((response) => {
                this.msgSuccess('新增成功')
                this.open = false
                this.getList()
              })
            }
          }
        })
      },
      /** 删除按钮操作 */
      handleDelete(row) {
        const supplierAddressUids = row.supplierAddressUid || this.ids
        const name = row.name || this.names
        this.$confirm(
          '是否确认删除地址编号为"' + name + '"的数据项?',
          '警告',
          {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning'
          }
        )
          .then(function () {
            return delAddress(supplierAddressUids)
          })
          .then(() => {
            this.getList()
            this.msgSuccess('删除成功')
          })
      },
      /** 导出按钮操作 */
      handleExport() {
        this.download(
          'supplier/address/export',
          {
            ...this.queryParams
          },
          `supplier_address.xlsx`
        )
      },
      // 状态修改
      handleStatusChange(row) {
        let text = row.dataStatus === '0' ? '无效' : '有效'
        this.$confirm('确定要把该地址设为' + text + '吗？', '警告', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        })
          .then(function () {
            return updateDataStatus(row.supplierAddressUid, row.dataStatus,row.supplierUid)
          })
          .then(() => {
            this.msgSuccess('操作成功')
            this.getList()
          })
          .catch(function () {
            row.dataStatus = row.dataStatus === '0' ? '1' : '0'
          })
      }
    }
  }
</script>

<style scoped>
  .MmsUiBlockTitle___line___27lhp1-44-1 {
    display: inline-block;
    background: #407cff;
    width: 2px;
    height: 16px;
    margin-right: 8px;
  }

  .MmsUiBlockTitle___title___2C-H-1-44-1 {
    display: inline-block;
    font-size: 20px;
    color: #333;
    position: relative;
  }
</style>
